# Copyright (C) 2009,2010,2011,2012  Marco Restelli
#
# This file is part of:
#   FEMilaro -- Finite Element Method toolkit
#
# FEMilaro is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# FEMilaro is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FEMilaro; If not, see <http://www.gnu.org/licenses/>.
#
# author: Marco Restelli                   <marco.restelli@gmail.com>

# List of the object files of the present folder
OBJ_TEST= mod_cavity.o \
     mod_squeeze.o \
     mod_poiseuille.o \
     mod_boxflow.o \
     mod_cylinder.o
OBJ_DGTEST= mod_warmbubble1.o \
     mod_breaking_wave.o \
     mod_boulder_windstorm.o \
     mod_channel.o \
     mod_turb_channel.o \
     mod_straka1993.o \
     mod_gravity_wave.o \
     mod_volcano.o
OBJ= mod_cg_ns_setup.o \
     mod_cg_ns_locmat.o \
     mod_cg_ns_linsystem.o \
     mod_cg_ns_ode.o \
     mod_testcases.o \
     $(OBJ_TEST)
OBJ_CGSTOKES= cg_stokes.o \
     mod_error_norms.o \
     $(OBJ)
OBJ_NS= cg_ns.o \
     mod_error_norms.o \
     $(OBJ)
OBJ_DGCOMP= dg_comp.o \
     mod_physical_constants.o \
     mod_atm_refstate.o \
     mod_dgcomp_flowstate.o \
     mod_dgcomp_testcases.o \
     mod_turb_flux.o \
     mod_viscous_flux.o \
     mod_smagorinsky_flux.o\
     mod_turb_flux_base.o \
     mod_dgcomp_rhs.o \
     mod_dgcomp_ode.o \
     mod_dgcomp_sponges.o \
     mod_dgcomp_statistics.o \
     $(OBJ_DGTEST)

# General settings -----------------------------------

PWD:=$(shell pwd)
BUILDDIR:=$(BUILDDIR)/NS

LIBS:=-lfml_general_utilities -lfml_linear_solvers -lfem $(LIBS)
LIBS_CGNS:=-lode $(LIBS)
LIBS_DGCOMP:=-lode $(LIBS)
LDFLAGS:=$(LDFLAGS) -L$(LIBDIR)

# VPATH includes:
#   PWD for the source files (included by default)
#   PWD/testcases for the source files of the testcases
#   BUILDDIR for the .o prerequisites
#   LIBDIR for the various libraries
#   BINDIR for the final executable
VPATH:=./testcases $(BUILDDIR) $(LIBDIR) $(BINDIR)

# clear unfinished targets
.DELETE_ON_ERROR:
# define the phony targets
.PHONY: clean

# Main targets ---------------------------------------

cg-stokes: $(OBJ_CGSTOKES)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_CGSTOKES) $(LIBS) -o $(BINDIR)/cg-stokes

cg-ns: $(OBJ_NS)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_NS) $(LIBS_CGNS) -o $(BINDIR)/cg-ns

dg-comp: $(OBJ_DGCOMP)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_DGCOMP) $(LIBS_DGCOMP) -o $(BINDIR)/dg-comp

# General rule: all the .o depend on the various libraries
%.o: %.f90 libfml_general_utilities.a libode.a libfml_linear_solvers.a libfem.a
	cd $(BUILDDIR) && \
	  $(FC) $(FFLAGS) $(MODFLAG)$(LIBDIR) -c $(PWD)/$<

# Some .o also depend on the libode library
mod_dgcomp_ode.o dg-comp.o mod_cg_ns_ode.o cg-ns.o: libode.a

# Some .o also depend on the libfml_linear_solvers library
mod_cg_ns_setup.o mod_cg_ns_linsystem.o mod_cg_ns_ode.o cg-ns.o: libfml_linear_solvers.a

clean:
	$(RM) $(BUILDDIR)/*.o $(BUILDDIR)/*.mod \
	  $(BINDIR)/cg-stokes $(BINDIR)/cg-ns $(BINDIR)/dg-comp

# Specific dependencies: because of USE directives ---
# Notice: here we list only the prerequisites which are not already
# included in the libfml_general_utilities.a library (nor in any other
# library), which is a prerequisite of all the .o files listed in this
# Makefile

mod_testcases.o: $(OBJ_TEST)

mod_cg_ns_locmat.o: \
   mod_testcases.o

mod_cg_ns_setup.o: \
   mod_testcases.o

mod_cg_ns_linsystem.o: \
   mod_testcases.o \
   mod_cg_ns_locmat.o

mod_cg_ns_ode.o: \
   mod_testcases.o \
   mod_cg_ns_setup.o \
   mod_cg_ns_locmat.o \
   mod_cg_ns_linsystem.o

cg_stokes.o: \
   mod_cg_ns_setup.o \
   mod_cg_ns_locmat.o \
   mod_cg_ns_linsystem.o \
   mod_testcases.o \
   mod_error_norms.o

cg_ns.o: \
   mod_testcases.o \
   mod_cg_ns_setup.o \
   mod_cg_ns_locmat.o \
   mod_cg_ns_linsystem.o \
   mod_cg_ns_ode.o \
   mod_error_norms.o

mod_atm_refstate.o: \
   mod_dgcomp_testcases.o

mod_warmbubble1.o: \
   mod_physical_constants.o

mod_breaking_wave.o: \
   mod_physical_constants.o

mod_boulder_windstorm.o: \
   mod_physical_constants.o

mod_channel.o: \
   mod_physical_constants.o

mod_turb_channel.o: \
   mod_physical_constants.o

mod_dgcomp_flowstate.o: \
   mod_atm_refstate.o

mod_dgcomp_testcases.o: \
   mod_physical_constants.o \
   $(OBJ_DGTEST)

mod_turb_flux.o: \
   mod_turb_flux_base.o \
   mod_viscous_flux.o \
   mod_smagorinsky_flux.o\
   mod_dgcomp_testcases.o

mod_viscous_flux.o: \
   mod_turb_flux_base.o \
   mod_dgcomp_testcases.o

mod_smagorinsky_flux.o: \
   mod_turb_flux_base.o \
   mod_dgcomp_testcases.o\
   mod_viscous_flux.o

mod_dgcomp_rhs.o: \
   mod_dgcomp_testcases.o \
   mod_atm_refstate.o \
   mod_dgcomp_flowstate.o \
   mod_turb_flux.o

mod_dgcomp_ode.o: \
   mod_turb_flux.o \
   mod_dgcomp_rhs.o

mod_dgcomp_sponges.o: \
   mod_dgcomp_testcases.o \
   mod_atm_refstate.o

mod_dgcomp_statistics.o: \
   mod_atm_refstate.o \
   mod_dgcomp_testcases.o \
   mod_dgcomp_rhs.o \
   mod_dgcomp_ode.o

dg_comp.o: \
   mod_physical_constants.o \
   mod_atm_refstate.o \
   mod_dgcomp_flowstate.o \
   mod_dgcomp_testcases.o \
   mod_turb_flux.o \
   mod_dgcomp_rhs.o \
   mod_dgcomp_ode.o \
   mod_dgcomp_sponges.o \
   mod_dgcomp_statistics.o

